package com.sangmo.fts.process.storage.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.fts.basic.model.FileUpload;
import com.sangmo.fts.basic.service.FileUploadService;
import com.sangmo.fts.process.storage.model.CsfAgreement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CsfAgreementDocumentService {

    @Autowired
    private CsfAgreementEntityService agreementEntityService;

    @Autowired
    private FileUploadService fileUploadService;

    public FileUpload createBuyerConfirmDoc(Long agreementId, Long fileUploadId) {
        CsfAgreement agreement = agreementEntityService.get(agreementId);
        if (agreement.isBuyerConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.csfAgreement.upload.permit.disabled");
        }
        agreement.setBuyerConfirmDoc(fileUploadId);
        agreementEntityService.save(agreement);
        return create(agreementId, fileUploadId, "CsfAgreement.Buyer");
    }

    public FileUpload createSellerConfirmDoc(Long agreementId, Long fileUploadId) {
        CsfAgreement agreement = agreementEntityService.get(agreementId);
        if (agreement.isSellerConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.csfAgreement.upload.permit.disabled");
        }
        agreement.setSellerConfirmDoc(fileUploadId);
        agreementEntityService.save(agreement);
        return create(agreementId, fileUploadId, "CsfAgreement.Seller");
    }

    public FileUpload createBankConfirmDoc(Long agreementId, Long fileUploadId) {
        CsfAgreement agreement = agreementEntityService.get(agreementId);
        if (agreement.isBankConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.csfAgreement.upload.permit.disabled");
        }
        agreement.setBankConfirmDoc(fileUploadId);
        agreementEntityService.save(agreement);
        return create(agreementId, fileUploadId, "CsfAgreement.Bank");
    }

    public FileUpload createStorageConfirmDoc(Long agreementId, Long fileUploadId) {
        CsfAgreement agreement = agreementEntityService.get(agreementId);
        if (agreement.isStorageConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.csfAgreement.upload.permit.disabled");
        }
        agreement.setStorageConfirmDoc(fileUploadId);
        agreementEntityService.save(agreement);
        return create(agreementId, fileUploadId, "CsfAgreement.Storage");
    }

    public FileUpload create(Long agreementId, Long fileUploadId, String refType) {
        FileUpload upload = fileUploadService.updateRef(fileUploadId, ""+agreementId, refType);
        return upload;
    }
}
